package io.renren.modules.backstage.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.exception.Result;
import io.renren.common.exception.StatusCode;
import io.renren.common.utils.OSSClientUtil;
import io.renren.modules.backstage.feign.CourseFeignClients;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.modules.backstage.dao.CourseDao;
import io.renren.modules.backstage.entity.CourseEntity;
import io.renren.modules.backstage.service.CourseService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Resource
    private CourseDao courseDao;

    @Resource
    private CourseFeignClients courseFeignClients;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        List<com.tencent.search.pojo.CourseEntity> list = courseFeignClients.searchFromEs(params);
        List<CourseEntity> list1 = JSON.parseArray(JSON.toJSONString(list), CourseEntity.class);
        IPage<CourseEntity> page = new Page<>(Integer.parseInt(params.get("page").toString()) - 1, Integer.parseInt(params.get("limit").toString()));

        return new PageUtils(page,list1);
    }

    @Override
    public Result searchCourse(Map<String, Object> paramMap) {
        Map<String, Object> resultMap = courseFeignClients.searchCourse(paramMap);

        return new Result(StatusCode.OK,"ES查询成功",true,resultMap);
    }

    @Override
    public Result uploadImage(MultipartFile file) {
        String ImagePath = UUID.randomUUID().toString();
        try {
            HashMap<String, Object> hashMap = OSSClientUtil.uploadObject2OSS(file, "coursePictures/" + ImagePath);
            String uploadId = (String) hashMap.get("uploadId");
            String ObjectName = (String) hashMap.get("uploadId:" + uploadId);
            String url = OSSClientUtil.getUrl("1908asyh", ObjectName);
            return new Result(StatusCode.OK,"图片上传成功",true,url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(StatusCode.ERROR,"图片上传失败",false);
    }

}